package com.samuel.basics.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.samuel.basics.GeneratorCode;
import com.samuel.basics.annotation.OperateLog;
import com.samuel.basics.base.BaseController;
import com.samuel.basics.bean.Context;
import com.samuel.basics.bean.GcBean;
import com.samuel.basics.constant.CommonConstant;
import com.samuel.basics.constant.OperateConstant;
import com.samuel.basics.utils.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author kaiji
 */
@Controller
@RequestMapping("code")
public class GeneratorController extends BaseController {

	public static final Logger logger = LoggerFactory.getLogger(GeneratorController.class);

	@Value("${spring.datasource.url}")
	public String url;
	@Value("${spring.datasource.driver-class-name}")
	public String driverName;
	@Value("${spring.datasource.username}")
	public String username;
	@Value("${spring.datasource.password}")
	public String password;

	/** 进入生成代码页面 */
	@GetMapping("")
	public String code() {
		return "/code/code";
	}

	@ResponseBody
	@PostMapping("execute")
	@OperateLog(operateModule = "生成代码", operateType = OperateConstant.SAVE, operateDesc = "生成代码与页面")
	public JSONObject execute(GcBean gb, String bizChName, String menuId) {
		JSONObject jsonObject = new JSONObject();
		try {
			if (StringUtils.isBlank(bizChName)) {
				jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
				jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "请填写中文业务描述");
				return jsonObject;
			}

			if (StringUtils.isBlank(menuId)) {
				jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
				jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "请选择父级菜单");
				return jsonObject;
			}

			GeneratorCode gc = new GeneratorCode();
			gb.setUrl(url);
			gb.setDriverName(driverName);
			gb.setUsername(username);
			gb.setPassword(password);
			String[] filedIds = new String[] {"id", "deleted", "version", "modify_date", "create_date"};
			gb.setCommonFiled(filedIds);
			gb.setMapperSwitch(false);
			Context context = new Context();
			context.setBizChName(bizChName);
			String enName = gb.getTableName().replaceAll(gb.getTablePrefix(), "");
			context.setBizEnBigName(enName.substring(0, 1).toUpperCase()+enName.substring(1));
			context.setBizEnName(enName);
			context.setMenuId(menuId);
			logger.info("context is : {}", JsonUtil.toJson(context));
			gb.setContext(context);
			gc.generatorCode(gb);
			jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.SUCCESS_CODE);
			jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "生成代码成功！");
		} catch (Exception e) {
			jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
			jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "生成代码失败！原因：" + e);
		}
		return jsonObject;
	}
}
